package com.wolf.controller;

import com.wolf.pojo.Wl_activity_config;
import com.wolf.pojo.Wl_user_admin;
import com.wolf.service.Wl_activity_configService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class Wl_activity_configController {
    @Autowired
    Wl_activity_configService wl_activity_configService;

    /**
     * 查询所有
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_activity_config_getAll")
    public String getAll(ModelMap modelMap){
        //获取状态
        select(modelMap);

        //获取所有信息
        List<Wl_activity_config>list = wl_activity_configService.getAll();
        modelMap.addAttribute("wl_activity_config_getAllList",list);
        return "table_Wl_activity_config";
    }

    /**
     * 模糊查询
     */
    @RequestMapping("wl_activity_config_fuzzyQuery")
    public String fuzzyQuery(ModelMap modelMap,Integer status,String activityname){
        //获取状态
        select(modelMap);

        //获取所有信息
        List<Wl_activity_config>list = wl_activity_configService.fuzzyQuery(status,activityname);
        modelMap.addAttribute("wl_activity_config_getAllList",list);
        return "table_Wl_activity_config";
    }

    /**
     * 删除以及发送修改状态
     */
    @RequestMapping("wl_activity_config_remove")
    public String remove(Integer status,String id){
        //获取所有信息
        if (status==1){
            status=0;
        }else {
            status=1;
        }
        int num = wl_activity_configService.remove(status,id);
        if(num>0){
            return "redirect:wl_activity_config_getAll";
        }else {
            return "500";
        }
    }

    /**
     * 修改
     */
    @RequestMapping("wl_activity_config_getById")
    public String update(ModelMap modelMap,String id){
        select(modelMap);

        Wl_activity_config wl = wl_activity_configService.selectByPrimaryKey(id);
        modelMap.addAttribute("Wl_activity_config",wl);
        return "wl_activity_config_Update";
    }
    @RequestMapping("wl_activity_config_update")
    public String update(Wl_activity_config wl_activity_config){
        int num = wl_activity_configService.updateByPrimaryKeySelective(wl_activity_config);
        if(num>0){
            return "redirect:wl_activity_config_getAll";
        }else {
            return "500";
        }
    }

    /**
     * 封装状态下拉框
     */
    private void select(ModelMap modelMap) {
        //获取状态
        List<Wl_activity_config> StatusList = wl_activity_configService.getByStatus();
        modelMap.addAttribute("wl_activity_config_StatusList",StatusList);
    }

    /**
     * 新增
     */
    @RequestMapping("wl_activity_config_insert")
    public String insert(Wl_activity_config wl_activity_config,HttpServletRequest request){
        //设置主键的值 值为当前日期
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String now_date = df.format(new Date());
        wl_activity_config.setId(now_date);

        //获得创建人id
        Wl_user_admin wl_user_admin=(Wl_user_admin)request.getSession().getAttribute("wl_user_admin");
        wl_activity_config.setCreateuserid(wl_user_admin.getId());

        int num = wl_activity_configService.insertSelective(wl_activity_config);
        if(num>0){
            return "redirect:wl_activity_config_getAll";
        }else {
            return "500";
        }
    }
}
